package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.WdclwcxkyqdBaDetailBo;
import com.ruoyi.report_wjjhl.domain.vo.WdclwcxkyqdBaDetailVo;
import com.ruoyi.report_wjjhl.domain.WdclwcxkyqdBaDetail;
import com.ruoyi.report_wjjhl.mapper.WdclwcxkyqdBaDetailMapper;
import com.ruoyi.report_wjjhl.service.IWdclwcxkyqdBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 无机结合料稳定材料无侧限抗压强度试验详细Service业务层处理
 *
 * @author zyb
 * @date 2024-08-07
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class WdclwcxkyqdBaDetailServiceImpl implements IWdclwcxkyqdBaDetailService {

    private final WdclwcxkyqdBaDetailMapper baseMapper;

    /**
     * 查询无机结合料稳定材料无侧限抗压强度试验详细
     */
    @Override
    public WdclwcxkyqdBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询无机结合料稳定材料无侧限抗压强度试验详细列表
     */
    @Override
    public TableDataInfo<WdclwcxkyqdBaDetailVo> queryPageList(WdclwcxkyqdBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WdclwcxkyqdBaDetail> lqw = buildQueryWrapper(bo);
        Page<WdclwcxkyqdBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询无机结合料稳定材料无侧限抗压强度试验详细列表
     */
    @Override
    public List<WdclwcxkyqdBaDetailVo> queryList(WdclwcxkyqdBaDetailBo bo) {
        LambdaQueryWrapper<WdclwcxkyqdBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WdclwcxkyqdBaDetail> buildQueryWrapper(WdclwcxkyqdBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WdclwcxkyqdBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, WdclwcxkyqdBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShijh() != null, WdclwcxkyqdBaDetail::getShijh, bo.getShijh());
        lqw.eq(bo.getYangsqsjzl() != null, WdclwcxkyqdBaDetail::getYangsqsjzl, bo.getYangsqsjzl());
        lqw.eq(bo.getQinsqsjzl() != null, WdclwcxkyqdBaDetail::getQinsqsjzl, bo.getQinsqsjzl());
        lqw.eq(bo.getQinshsjzl() != null, WdclwcxkyqdBaDetail::getQinshsjzl, bo.getQinshsjzl());
        lqw.eq(bo.getYangsqjdzlsh() != null, WdclwcxkyqdBaDetail::getYangsqjdzlsh, bo.getYangsqjdzlsh());
        lqw.eq(bo.getXisl() != null, WdclwcxkyqdBaDetail::getXisl, bo.getXisl());
        lqw.eq(bo.getYangsqsjdgd() != null, WdclwcxkyqdBaDetail::getYangsqsjdgd, bo.getYangsqsjdgd());
        lqw.eq(bo.getQinshsjdgd() != null, WdclwcxkyqdBaDetail::getQinshsjdgd, bo.getQinshsjdgd());
        lqw.eq(bo.getJianczdyl() != null, WdclwcxkyqdBaDetail::getJianczdyl, bo.getJianczdyl());
        lqw.eq(bo.getWucxkyqd() != null, WdclwcxkyqdBaDetail::getWucxkyqd, bo.getWucxkyqd());
        return lqw;
    }

    /**
     * 新增无机结合料稳定材料无侧限抗压强度试验详细
     */
    @Override
    public Boolean insertByBo(WdclwcxkyqdBaDetailBo bo) {
        WdclwcxkyqdBaDetail add = BeanUtil.toBean(bo, WdclwcxkyqdBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改无机结合料稳定材料无侧限抗压强度试验详细
     */
    @Override
    public Boolean updateByBo(WdclwcxkyqdBaDetailBo bo) {
        WdclwcxkyqdBaDetail update = BeanUtil.toBean(bo, WdclwcxkyqdBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(WdclwcxkyqdBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除无机结合料稳定材料无侧限抗压强度试验详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
